package com.soft.idc.module.warnmanager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jetty.util.log.Log;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.CaptchaRender;
import com.soft.idc.common.control.AuthInterceptor;
import com.soft.idc.common.model.IdcOrg;
import com.soft.idc.common.model.IdcRole;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.model.IdcWarnType;
import com.soft.idc.common.util.JQList;
import com.soft.idc.common.util.JQPage;

@Before(AuthInterceptor.class)
public class WarnController extends Controller {

	public void index() {
		// IdcOrg org = (IdcOrg) this.getSessionAttr("SYS_USER_ORG");
		List<Record> list = Db.find("select warn_type_id,name from idc_warn_type i");
		this.setAttr("warn_type", list);
		this.renderJsp("/warnmanager/main.jsp");
	}

	public void loadWarnInfo() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		String warnType = this.getPara("warn_type");
		String warnLevel = this.getPara("warn_level");
		String beginTime = this.getPara("begin_time");
		String endTime = this.getPara("end_time");
		String isClear = this.getPara("isClear");

		IdcOrg org = (IdcOrg) this.getSessionAttr("SYS_USER_ORG");
		String sqlQuery = "select record_id,(select name from idc_device i where i.device_id = r.obj_id) as device_name, "
				+ "(select name from idc_device_type t where t.type_id = r.type_id) as device_type,r.warn_name, "
				+ "(select name from  idc_warn_type wt where wt.warn_type_id = r.warn_type ) as warn_type,count,first_time,"
				+ "r.warn_level,r.is_clear,clear_user_id,(select realname from idc_user i where i.user_id = r.clear_user_id ) as username,"
				+ "(select name from idc_org o where o.org_id = r.org_id) as org_name,clear_time";
		String sqlend = " from idc_warn_record r where obj_type = ? ";

		List<Object> params = new ArrayList<Object>();
		params.add(0);
		if (warnType != null && !"".equals(warnType)) {
			sqlend += " and warn_type = ? ";
			params.add(warnType);
		}

		if (warnLevel != null && !"".equals(warnLevel)) {
			sqlend += " and warn_level = ? ";
			params.add(warnLevel);
		}

		if (beginTime != null && !"".equals(beginTime)) {
			sqlend += " and first_time >= str_to_date(?,'%Y-%m-%d %H:%i:%s')";
			params.add(beginTime);
		}

		if (endTime != null && !"".equals(endTime)) {
			sqlend += " and first_time <= str_to_date(?,'%Y-%m-%d %H:%i:%s')";
			params.add(endTime);
		}

		if (isClear != null && !"".equals(isClear)) {
			sqlend += " and is_clear = ?";
			params.add(isClear);
		}

		if (org.getOrgId() != 1) {
			sqlend += " and org_id = ? ";
			params.add(org.getOrgId());
		}
		System.out.println(params.toString());
		int pageNum = start / length + 1;
		Page<Record> page = Db.paginate(pageNum, length, sqlQuery, sqlend, (Object[]) params.toArray());
		JQPage jPage = new JQPage(page);
		this.renderJson(jPage);
	}

	public void confirmWarn() {
		IdcUser u = (IdcUser) this.getSessionAttr("SYS_USER");
		String recordIds = this.getPara("recordIds");

		String[] ids = recordIds.split(",");
		for (String id : ids) {
			String sql = "update idc_warn_record r set r.is_clear = 1 ,clear_user_id= ?,clear_time=now() where r.record_id = ?";
			Db.update(sql, u.getUserId(), Integer.parseInt(id));
		}
		this.renderJson("result", "0");
	}
}
